package com.achen.controller;

import com.achen.constant.MessageConstant;
import com.achen.entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author achen
 * @create 2020-04-24 23:45
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/findUsername")
    public Result findUsername(){
        //当springsecurity完成认证后，会将当前用户信息保存到框架提供的上下文对象
        User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);
        //因为springsecurity中底层是将username存入session中的，有默认的到期时间
        if(user != null){
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,user.getUsername());
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

}
